<template>
    <el-dialog :title="isEdit?'编辑货类管理':'新增货类管理'" :visible.sync="dialogForm" width="680px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="货类名称" prop="cargoTypeName">
                        <el-input v-model="form.cargoTypeName" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="货类代码" prop="cargoTypeCode">
                        <el-input v-model="form.cargoTypeCode" @input="changeCode" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" :rows="3" style="width: 188px" placeholder="请输入内容"
                                  v-model.trim="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addCargoType, updateCargoType } from "@/api/baseResource/baseResource"
    import { maxLength, sortLength } from "@/utils/rules"
    import { PUBLIC_NOT_CHINESE_TWO } from '@/utils/Reg'

    export default {
        name: "cargoTypeForm",
        components: {},
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    cargoTypeName: [{ required: true, message: '请输入货类名称', trigger: 'blur' }, sortLength],
                    cargoTypeCode: [{ required: true, validator: PUBLIC_NOT_CHINESE_TWO, trigger: 'blur' }, sortLength],
                    remark: [maxLength]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    cargoTypeName: "",
                    cargoTypeCode: "",
                    remark: "",
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addCargoType(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateCargoType(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            changeCode(val){
                this.form.typeCode=val.toUpperCase()
            }
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
